﻿using NLog;
using System;
using System.Runtime.CompilerServices;
using System.Windows.Shapes;

namespace Common
{
    /// <summary>
    /// 增强版日志工具类（线程安全，支持结构化日志和智能诊断）
    /// </summary>
    public static class Mylog
    {
        //private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
        private static readonly Logger _logger = LogManager.GetLogger("");

        /// <summary>
        /// 记录信息级日志（自动捕获设备通信关键字段）
        /// </summary>
        public static void Info(
            string message,
            string deviceName = null,
            [CallerMemberName] string method = "",
            [CallerFilePath] string file = "",
            [CallerLineNumber] int line = 0
            )
        {
            var logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, message);
            logEvent.Properties["Device"] = deviceName;
            logEvent.Properties["Method"] = method;
            logEvent.Properties["Timestamp"] = DateTime.Now.ToString("HH:mm:ss.fff");
            logEvent.Properties["Source"] = $"{System.IO.Path.GetFileName(file)}:{line}";

            _logger.Log(logEvent);
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="deviceName"></param>
        /// <param name="method"></param>
        /// <param name="file"></param>
        /// <param name="line"></param>
        public static void Debug(
              string message,
              string deviceName = null,
              [CallerMemberName] string method = "",
              [CallerLineNumber] int line = 0,
              [CallerFilePath] string file = ""
              )
        {
            var logEvent = new LogEventInfo(LogLevel.Debug, _logger.Name, message);
            logEvent.Properties["Device"] = deviceName;
            logEvent.Properties["Method"] = method;
            logEvent.Properties["Timestamp"] = DateTime.Now.ToString("HH:mm:ss.fff");
            logEvent.Properties["Source"] = $"{System.IO.Path.GetFileName(file)}:{line}";

            _logger.Log(logEvent);
        }

        /// <summary>
        /// 记录设备通信数据（专为工业设备优化）
        /// </summary>
        public static void DeviceData(
            string deviceName = null,
            string sendData = null,
            string receivedData = null,
            object parsedValue = null,
            string unit = null,
            [CallerMemberName] string method = "",
            [CallerFilePath] string file = "",
            [CallerLineNumber] int line = 0
            )
        {
            var logEvent = new LogEventInfo(LogLevel.Debug, _logger.Name, $"Device Communication")
            {
                Properties =
                {
                    ["Device"] = deviceName,
                    ["Method"] = method,
                    ["Send"] = sendData,
                    ["Received"] = receivedData,
                    ["Timestamp"] = DateTime.Now.ToString("HH:mm:ss.fff"),
                    ["Value"] = parsedValue,
                    ["Unit"] = unit,
                    ["Source"] = $"{System.IO.Path.GetFileName(file)}:{line}"
                }
            };

            _logger.Log(logEvent);
        }

        /// <summary>
        /// 记录错误（自动提取异常关键信息）
        /// </summary>
        public static void Error(
            Exception ex,
            string message = null,
            string deviceName = null,
            [CallerMemberName] string method = "",
            [CallerFilePath] string file = "",
            [CallerLineNumber] int line = 0)
        {
            var logEvent = new LogEventInfo(LogLevel.Error, _logger.Name, message ?? ex.Message)
            {
                Exception = ex,
                Properties =
                {
                    ["Device"] = deviceName,
                    ["Method"] = method,
                    ["Timestamp"] = DateTime.Now.ToString("HH:mm:ss.fff"),
                    ["Source"] = $"{System.IO.Path.GetFileName(file)}:{line}",
                    ["ErrorType"] = ex.GetType().Name,
                    ["StackTrace"] = ex.StackTrace,
                    ["Method"] = method
                }
            };

            _logger.Log(logEvent);
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="deviceName"></param>
        /// <param name="method"></param>
        /// <param name="file"></param>
        /// <param name="line"></param>
        public static void Error(

            string message = null,
            string deviceName = null,
            [CallerMemberName] string method = "",
            [CallerFilePath] string file = "",
            [CallerLineNumber] int line = 0)
        {
            var logEvent = new LogEventInfo(LogLevel.Error, _logger.Name, message)
            {
                Properties =
                {
                    ["Device"] = deviceName,
                    ["Method"] = method,
                    ["Timestamp"] = DateTime.Now.ToString("HH:mm:ss.fff"),
                    ["Source"] = $"{System.IO.Path.GetFileName(file)}:{line}",

                    ["Method"] = method
                }
            };

            _logger.Log(logEvent);
        }

        /// <summary>
        /// 快速记录警告（支持条件触发）
        /// </summary>
        public static void WarnIf(
            bool condition,
            string message,
            string deviceName = null,
            [CallerMemberName] string method = "")
        {
            if (condition)
            {
                var logEvent = new LogEventInfo(LogLevel.Warn, _logger.Name, message)
                {
                    Properties =
                    {
                        ["Device"] = deviceName,
                        ["Method"] = method
                    }
                };
                _logger.Log(logEvent);
            }
        }
    }
}